package com.jsy.common.util.publicApi.response;





import java.io.Serializable;
import java.util.Date;

/**
 * 统一API响应结果封装
 */

public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer code;

    private String message;

    private T data;

    private Date timestamp;

    public Result() {
        this.timestamp =  new Date();
    }

    public Result(Integer code, String message, T data, Date now) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.timestamp = now;
    }

    // 成功响应（带数据）
    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data, new Date());
    }

    // 成功响应（不带数据）
    public static <T> Result<T> success() {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null, new Date());
    }

    // 成功响应（自定义消息）
    public static <T> Result<T> success(String message) {
        return new Result<>(ResultCode.SUCCESS.getCode(), message, null, new Date());
    }

    // 失败响应（带错误信息）
    public static <T> Result<T> failure(String message) {
        return new Result<>(ResultCode.FAILURE.getCode(), message, null, new Date());
    }

    // 失败响应（自定义错误码和信息）
    public static <T> Result<T> failure(Integer code, String message) {
        return new Result<>(code, message, null, new Date());
    }

    // 失败响应（枚举错误）
    public static <T> Result<T> failure(ResultCode resultCode) {
        return new Result<>(resultCode.getCode(), resultCode.getMessage(), null, new Date());
    }

    // 自定义构建
    public Result<T> code(Integer code) {
        this.code = code;
        return this;
    }

    public Result<T> message(String message) {
        this.message = message;
        return this;
    }

    public Result<T> data(T data) {
        this.data = data;
        return this;
    }
}